home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-01-12 | 4.4 KB | 102 lines | [TEXT/GEOL] |
- Item 1145388 9-Jan-90 20:11
-
- From: D3632 Cadence Design, Ken Friedenbach,PRT
-
- To: NASSI Nassi, Ike
- SCHMUCKER1 Schmucker, Kurt
- CPLUS.APPLE$ C++ Interest List--Apple Employees
- CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: Re-Re- What Gives Here?
-
- Kurt, Gary, Ike,
-
- Part of "What gives here?" is bugs in the current implementation, and part is
- weaknesses in the language definition. Pure virtual functions were added very
- late in the development cycle for cfront 2.0. So was the logic for checking
- for "hiding" errors. These two language features are not fully integrated yet.
-
- NOTE: There are other parts of the current compiler which do not fully "know"
- about pure virtual functions, such as destructors. Attempting to declare a
- pure virtual destructor results in a link error, when a derived class
- destructor attempts to call the (non-existent) base class destructor.
-
- If you omit the keyword "virtual" everywhere in your example, then "things work
- O.K.". For example, the following does not generate any compiler warnings:
-
- ----------
-
- class A {
- void foo(int& i) ;
- void foo(char& c) ;
- };
-
- class B : public A {
- void foo(int& i);
- void foo(char& c);
- void foo(float& f);
- void foo(double& d);
- };
-
- class C : public B {
- void foo(float& f);
- void foo(double& d);
- };
-
- -------------
-
- (Of course, it is probably a "BUG" to be trying to override non-virtual
- functions, but that is another, bigger language design issue. Should a
- language force a user to do "reasonable" things? Or, in the interest of "full
- orthogonality", allow a little non-sense along with the sense?)
-
- The warnings about "hiding" virtual functions is an attempt to cure a language
- design weakness with an ad-hoc mechanism for generating warnings. The weakness
- is caused by the looseness regarding the keyword "virtual", which is required
- for the first declaration of a virtual function in a base class, but can be
- omited for derived classes.
-
- This leads to several types of "architecture errors" which cannot be detected
- by a C++ compiler:
- 1. Inadvertant matching of a user function with a virtual function in a
- library base class. (The user causes an unexpected "replacement" of a base
- class function.)
- 2. Inadvertant mis-matching when a user attempts to override an existing
- function, but makes a spelling or type matching error. (The user get an
- unexpected "new" function, which can be either virtual or non-virtual,
- depending on whether or not the user opted to use the "virtual" keyword.)
- Mis-matches can be caused by spelling errors, inconsistant use of "const" or
- "unsigned", or other "minor" type mis-matches.
-
- The weakness of the language in this area was worsened in Version 2.0 which has
- automatic "overloading". Prior to Version 2.0, the keyword "overload" had to
- be used.
-
- The "best" solution to the language weakness would be to use the keyword
- "override" for derived classes. There are two ways override could be defined:
- 1. Weak semantics: allow override. Check that a match occurs if the
- keyword override is used. Otherwise, fall back to the current rules.
- 2. Strong semantics: require override. Check that matches do not occur
- if override has not been specified. Give an error if "virtual" is used a
- second time, or nothing is said for a match.
-
- Only the Strong semantics will catch all the errors mentioned above.
-
- Bjarne has indicated that he is not going to implement override, and the issue
- should be directed to the ANSI C++ Standards Committee for consideration.
-
- Note: There are other language design issues lurking here! In your example,
- the use of references makes all the types distinct. But what about C's rules
- for "widening" chars and shorts to int? or float and double to extended? And
- what about signed or unsigened vs. "plain" int? And what about "char const *"
- and "char * const" and "char const * const" as opposed to "char *"?
-
- Given the great number of distinct types in C (and C++), and the various rules
- for "converting", "widening", etc. it was probably a good idea to give
- "warnings" for overloadings which are "too close". Whether the rule about
- "overriding is stronger than overloading" implements this idea or not is
- debatable. And limiting the check to virtual functions seems like a mistake.
-
- Ken
-
-